package com.aifast.system.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.aifast.common.annotations.AiIdempotent;
import com.aifast.common.base.R;
import com.aifast.common.layui.LayuiDataTable;
import com.aifast.system.sys.entity.SysMessage;
import com.aifast.system.sys.service.SysMessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import com.aifast.common.base.Controller;
/**
 * 站内信 控制层
 *
 * @author fujian
 * @date 2022-10-19
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/message")
public class SysMessageController extends Controller {

    private final SysMessageService service;

    @SaCheckPermission("message:list")
    @GetMapping("list")
    public LayuiDataTable list() {
        return service.list(getInt("type"), get("message"), getPage());
    }

    @AiIdempotent
    @SaCheckPermission(value = {"message:add", "message:update"}, mode = SaMode.OR)
    @PostMapping("add")
    public R add(@Valid SysMessage sysMessage) {
        return service.insertOrUpdate(sysMessage) ? OK() : ERROR();
    }

    @AiIdempotent
    @SaCheckPermission("message:del")
    @GetMapping("del")
    public R del() {
        return service.del(getInt("id"), get("ids")) ? OK() : ERROR();
    }

    @GetMapping("info")
    public R info() {
        return service.info(getInt("id"));
    }

    @SaCheckPermission("message:export")
    @GetMapping("export")
    public void export() {
        service.export(getInt("type"), get("message"), response);
    }

    @SaCheckPermission("message:record")
    @GetMapping("getRecord")
    public LayuiDataTable getRecord(){
        return service.getRecord(get("userId"),getInt("messageId"),get("sendTime"),get("read"),getPage());
    }

    @GetMapping("read")
    public void read(){
        service.read(getInt("userId"),getInt("messageId"));
    }


}
